package unit7;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MapDemo {
    Person[] people = {
        new Person("张三", 1.7, 65),
		new Person("李四", 1.6, 60),
		new Person("王五", 1.8, 70)
    };
    List<Person> list = new ArrayList<>();
    // 姓名 -> 人
    Map<String, Person> map = new HashMap<>();

    MapDemo() {
        for (Person p : people) {
            list.add(p);
            map.put(p.getName(), p);
        }
    }

    // 按姓名顺序查找
    Person findListByName(String name) {
        for (Person p : people) {
            if  (p.getName().equals(name)) {
                return p;
            }
        }
        return null;
    }

    Person findMapByName(String name) {
        return  map.get(name);
    }

    public static void main(String[] args) {
        MapDemo mapDemo = new MapDemo();
        System.out.println(mapDemo.list);
        System.out.println();

        // 查找李四
        System.out.print("在list中查找李四");
        Person lisi = mapDemo.findListByName("李四");
        System.out.println(lisi);
        System.out.println();

        System.out.print("在map中查找王五");
        Person wangwu = mapDemo.findMapByName("王五");
        System.out.println(wangwu);
    }

}
